package tool;


import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

/**
 * Tools: Functions that may be use to help !
 * 
 * @author CORSINI Pierre, Cyril CHANDELIER, Sylvain BRUN
 * @version January 25, 2010
 */
public class Tools {


	/**
	 * Delete the file at the given path
	 * 
	 * @param path The path
	 * @return True if success
	 */
	public static Boolean deleteFile(String path) {
		try {
			return new File(path).delete();
		} catch (Exception e) {
			return false;
		}
	}

	/**
	 * Return the given string with a upper case first char
	 * @param s
	 * @return the resulting string
	 */
	public static String firstLetterUp(String s) {
		if (s.length() < 1 || s == null)
			return s;
		else
			return Character.toUpperCase(s.charAt(0)) + s.substring(1);
	}

	/**
	 * Get the execution path of the current application
	 * @return
	 */
	public static String getExecutionPath() {
		try {
			return System.getProperty("user.dir");
		} catch (Exception e) {
			return null;
		}

	}

	/**
	 * Read the content of a file
	 * 
	 * @param f The File
	 * @return A string containing the file's content
	 * @throws IOException
	 */
	public static String readFile(File f) throws IOException {
		FileReader fr = new FileReader(f);
		BufferedReader br = new BufferedReader(fr);
		String ret = "", nextLine;
		while ((nextLine = br.readLine()) != null)
			ret += nextLine + "\n";

		fr.close();
		br.close();
		return ret;
	}



	/**
	 * Write a String in a file (override)
	 * 
	 * @param f The File
	 * @param s The String
	 * @return True if succeed
	 */
	public static Boolean writeFile(File f, String s) {
		try {
			FileWriter fr = new FileWriter(f);
			BufferedWriter bw = new BufferedWriter(fr);
			bw.write(s);
			bw.close();
			fr.close();
		} catch (IOException e) {
			return false;
		}
		return true;
	}

}
